<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ include file="header.jsp" %>
<%@ page import="java.util.*" %>  
<%@page import="java.util.Enumeration"%><html>
<%@ page import="au.edu.unsw.editingtool.global.GlobalConstant" %>
<h2>Claim Review</h2>
<script type="text/javascript">

function GetXmlHttpObject()
{
	var http_request=null;
	if (window.XMLHttpRequest)
	{ // Mozilla, Safari,...
        http_request = new XMLHttpRequest();
        if (http_request.overrideMimeType)
        {
                http_request.overrideMimeType('text/xml');
        }
	}
	else if (window.ActiveXObject)
	{ // IE
        try {http_request = new ActiveXObject("Msxml2.XMLHTTP");}
        catch (e)
        {
                try{http_request = new ActiveXObject("Microsoft.XMLHTTP");}
                catch (e) {alert('Unfortunatelly you browser doesn\'t support this feature.');return null;}
        }
	}
	if (!http_request)
	{
        alert('Unfortunatelly you browser doesn\'t support this feature.');
        return null;
	}

	return http_request;
}

function setClaimStatus(cuser,cpid,cstatus)
{
	var url = "/EditingTool/ClaimReview?ctrl=<%=GlobalConstant.cmdClaimStatusUpdate%>&<%=GlobalConstant.username%>="+cuser+"&<%=GlobalConstant.pid%>="+cpid+"&<%=GlobalConstant.paramClaimStatus%>="+cstatus;
	var http_request = GetXmlHttpObject();

	if (http_request==null)
	{
        alert('Unfortunatelly you browser doesn\'t support this feature.');
        return;
	}
	
    http_request.onreadystatechange = function()
    {
            if (http_request.readyState == 4)               
            {
                    if (http_request.status == 200)
                    {
                            returndata = http_request.responseText;
                            //var x=document.getElementById('debugresult');  
                            //x.innerHTML=returndata;  
                            var patt=new RegExp("ERROR: ");
							if(patt.test(returndata))							
								alert(returndata+" Please contact system admin.");                             
                    }
                    else
                    {
                            alert('There was a problem with the request.(Code: ' + http_request.status + ')');
                    }
            }
    }
    http_request.open('GET', url, true);
    http_request.send(null);
}

//Set status to call ajax for updating claim table
function checkStatus(element,cuser,cpid)
{
	var name = element.id;
	if(element.checked)
	{		
		setClaimStatus(cuser,cpid,"<%=GlobalConstant.claimClaimed %>");
		//alert("CLAIMED");
	}
	else
	{		
		setClaimStatus(cuser,cpid,"<%=GlobalConstant.claimRequest %>")
		//alert("REQUEST");
	}
}

//Delete table row and call ajax to delete claim table.
function deleteClaimRow(tablename, rowname)
{	
	var tableObject = document.getElementById(tablename);	
	var template = rowname.replace("<%=GlobalConstant.paramClaimSearchTemplate %>",'');
	var userpid = template.split("_");

	var url = "/EditingTool/ClaimReview?ctrl=<%=GlobalConstant.cmdClaimDelete%>&<%=GlobalConstant.username%>="+userpid[0]+"&<%=GlobalConstant.pid%>="+userpid[1];
	var http_request = GetXmlHttpObject();
	
    if (http_request==null)
    {
            alert('Unfortunatelly you browser doesn\'t support this feature.');
            return false;
    }
    http_request.onreadystatechange = function()
    {
            if (http_request.readyState == 4)
            {
                    if (http_request.status == 200)
                    {
                            returndata = http_request.responseText;
                            //var x=document.getElementById('debugresult');  
                            //x.innerHTML=returndata;                 
                            var patt=new RegExp("ERROR: ");
							if(!patt.test(returndata))							
								deleteClaimSearchTableRow(tablename, rowname);
							else
								alert(returndata+" Please contact system admin.");
                    }
                    else
                    {alert('There was a problem with the request.(Code: ' + http_request.status + ')');}
            }
    }
    http_request.open('GET', url, true);
    http_request.send(null);
}

function deleteClaimSearchTableRow(tname, rname)
{
	var tableObject = document.getElementById(tname);
	var tableRow = tableObject.rows;
	if(!tableRow)
		return false;
	for(var ti=0; ti < tableRow.length;ti++)
	{
		//alert(tableRow[ti].id+" "+tableRow.length+": "+ti);
		if(tableRow[ti].id==rname)
		{
			document.getElementById(tname).deleteRow(ti);                        				
			return true;
		}
	}
}
</script>
<div>
<span id="debugresult"></span>
</div>
<%
ArrayList claimUsername = (ArrayList)request.getAttribute(GlobalConstant.paramClaimClaimedAll);
if(claimUsername!=null)
{	
%>
<form method="post" action="/EditingTool/ClaimReview" class="review">		
<%	
	if(claimUsername.size()!=0)
	{
		for(int i = 0;i<claimUsername.size();i++)
		{			
			ArrayList claimpids = (ArrayList) request.getAttribute(GlobalConstant.paramClaimUserTemplate+claimUsername.get(i).toString());
			
			if(claimpids==null)
				continue;
			if(claimpids.size()==0)
				continue;
%>
<fieldset>
<legend>Username : <%=claimUsername.get(i) %></legend>
<br>
<table border="1">
<tr>
	<th>Resource ID:</th><th>Approve?</th>
</tr>
<%
			for(int j = 0;j<claimpids.size();j++)
			{
%>
<tr>
	<td>
		<a href="Edit?pid=<%=claimpids.get(j) %>&ctrl=edit"><%=claimpids.get(j) %></a>
	</td>
	<td>
		<input type="checkbox" name="<%=GlobalConstant.paramClaimPidTemplate+claimpids.get(j).toString().replace(':','_') %>" 
			value="<%=claimUsername.get(i)%>" />
	</td>
</tr>
<%				
			}
%>
</table>
</fieldset>
<Br>
<br>
<%
		}
	}
%>

		<table class="listing" width="80%" border="0">
		<tr><th><b>Continue or Return to Deposit Form</b></th></tr>
		<tr><td><i>To continue with this deposit, click 'confirm' below. If you wish to make changes, please click 'edit'.</i></td></tr>
		<tr>
			<td>
				<input type="submit" name="submit" value="Confirm" onClick=""/> 	
				<input type="hidden" name="<%=GlobalConstant.requestCommand%>" value="<%=GlobalConstant.cmdClaimReviewUpdate %>">															
			</td>	
		</tr>
		</table>
</form>
<%		
}
else
{
%>
No Claim to be approved. <br>
<%
}
%>
<Br>
<br>
		<table id="tableSearchClaimStatus" class="listing" border="0">
		<tr><th colspan="3"><b>Search Claim Status</b></th></tr>
<%
LinkedList searchClaims = (LinkedList)request.getAttribute(GlobalConstant.paramClaimAll);
if(searchClaims!=null)
{
	if(searchClaims.size()!=0)
	{
%>
		<tr><th>Username:</th><th>Publication ID</th><th>Status</th></tr>	
<%		
		for(int si = 0;si<searchClaims.size();si++)
		{		
			String[] asc = (String[]) searchClaims.get(si);
			if(asc.length==3)
			{
				String checked="";
				if(asc[2].compareTo(GlobalConstant.claimClaimed)==0)
				{	checked="checked=\"true\""; }
%>
		<tr id="<%=GlobalConstant.paramClaimSearchTemplate+asc[0]+"_"+asc[1]%>">
			<td>
				<%=asc[0] %>
			</td>
			<td>
				<%=asc[1] %>
			</td>
			<td>
				<input type="checkbox"
				value="" 
				<%=checked %> 
				onClick="javascript:checkStatus(this,'<%=asc[0] %>','<%=asc[1] %>');"/>
			</td>
			<td>
				<input type="button" 
					onclick="javascript:deleteClaimRow('tableSearchClaimStatus', '<%=GlobalConstant.paramClaimSearchTemplate+asc[0]+"_"+asc[1]%>');" 
					value="delete" />				
			</td>
		</tr>
<%			
			}
		}
	}
}
%>
		<tr><td colspan="4"></td></tr>
		<tr>
			<td colspan="4">
			<form method="post" action="/EditingTool/ClaimReview" class="review">
			<input type="text" name=<%=GlobalConstant.paramClaimSearchText%> class="title" id="title" size=70 onChange="" /><br>		
			<select name="<%=GlobalConstant.paramclaimSearchType %>" onchange="">
			<option value="<%=GlobalConstant.paramClaimUsername %>">User</option>             
			<option value="<%=GlobalConstant.paramClaimPid %>">Publication ID</option>
            </select>
			<input type="submit"  value="Search">
			<input type="hidden" name="<%=GlobalConstant.requestCommand%>" value="<%=GlobalConstant.cmdClaimReview %>">							
			</form>
			</td>	
		</tr>
		</table>		
<a href="Main">Back to the main page</a><br>
<%@ include file="footer.jsp" %>